package wang.hu.bos.web.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.poi.ss.formula.functions.T;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import wang.hu.bos.domain.take_delivery.WayBill;
import wang.hu.bos.service.WayBillService;

import com.opensymphony.xwork2.ActionContext;

@Controller
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
public class WayBillAction extends BaseAction<WayBill> {
	@Autowired
	private WayBillService wayBillService;
	public static final Logger LOGGER = Logger.getLogger(WayBill.class);
	private Specification<T> specification;

	@Action(value = "wayBill_save", results = { @Result(name = "success", type = "json") })
	public String save() {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			model.setSignStatus(1);
			wayBillService.save(model);
			result.put("success", true);
			result.put("msg", "运单保存成功");
			LOGGER.info("运单号为:" + model.getWayBillNum() + "运单保存成功!");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "运单保存失败");
			LOGGER.info("运单号为:" + model.getWayBillNum() + "运单保存失败!");

		}
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}

	// 此方法只返回所有待发货状态的运单数据
	@Action(value = "wayBill_pageData", results = { @Result(name = "success", type = "json") })
	public String pageData() {
		Pageable pageable = new PageRequest(page - 1, rows);

		Page<WayBill> pagedata = wayBillService.pageData(pageable, model);
		pushPageableToValueStack(pagedata);
		return SUCCESS;
	}

	@Action(value = "wayBill_findByWayBillNum", results = { @Result(name = "success", type = "json") })
	public String findByWayBillNum() {
		WayBill wayBill = wayBillService
				.findByWayBillNum(model.getWayBillNum());
		Map<String, Object> result = new HashMap<String, Object>();
		if (wayBill != null) {
			result.put("success", true);
			result.put("wayBillData", wayBill);
		} else {
			result.put("success", false);
		}
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}

}
